package 其他;

import javax.xml.transform.Source;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;

public class 合同计算器 {

    public static void main(String[] args) {
        A a = new A();
        a.totalAmount = new BigDecimal("5000");
        a.monthlyInterest = new BigDecimal("0.07");
        a.periods = 7;


        ArrayList<B> bs = get(a);
        for (B b : bs) {
            System.out.println(b);
        }

    }

    /**
     * 93.58
     * @param a
     * @return
     */
    public static ArrayList<B> get(A a) {
        ArrayList<B> al = new ArrayList();
        BigDecimal totalAmount = a.totalAmount;
        BigDecimal totalAmount1 = totalAmount;
        BigDecimal monthlyInterest = a.monthlyInterest;
        Integer periods = a.periods;
        LocalDate now = LocalDate.now();
        BigDecimal monthlyPayments = (totalAmount.multiply(monthlyInterest)
                .multiply(new BigDecimal("1").add(monthlyInterest).pow(periods)))
                .divide(new BigDecimal("1").add(monthlyInterest).pow(periods).subtract(new BigDecimal("1"))
                        ,2, RoundingMode.HALF_UP);
        for (Integer i = 1; i <= periods; i++) {
            if (i == periods){

            }else {
                LocalDate paymentTime = now.plusMonths(i-1).with(TemporalAdjusters.lastDayOfMonth());
                B b = new B();
                b.periods = i;
                b.paymentTime = paymentTime;
                b.monthlyPayments = monthlyPayments;
                b.interest = totalAmount.multiply(monthlyInterest).setScale(2, RoundingMode.HALF_UP);
                b.principal = monthlyPayments.subtract(b.interest);
                totalAmount = totalAmount.subtract(b.principal);
                b.remainingPrincipal = totalAmount;
                b.paidPrincipal = totalAmount1.subtract(totalAmount);
                al.add(b);
            }

        }
        return al;
    }
}


class A{

    BigDecimal totalAmount;//总金额

    BigDecimal monthlyInterest;//月息

    Integer periods;


    @Override
    public String toString() {
        return "A{" +
                "totalAmount=" + totalAmount +
                ", monthlyInterest=" + monthlyInterest +
                ", periods=" + periods +
                '}';
    }
}

class B{
    Integer periods;

    BigDecimal monthlyPayments; //月还款

    BigDecimal principal;  //本金

    BigDecimal interest;  //利息

    BigDecimal paidPrincipal; //已还本金

    BigDecimal remainingPrincipal; //剩余本金

    LocalDate paymentTime;

    @Override
    public String toString() {
        return "B{" +
                "periods=" + periods +
                ", monthlyPayments=" + monthlyPayments +
                ", principal=" + principal +
                ", interest=" + interest +
                ", paidPrincipal=" + paidPrincipal +
                ", remainingPrincipal=" + remainingPrincipal +
                ", paymentTime=" + paymentTime +
                '}';
    }
}
